<?php

/**
 * 文件由尚数增量代码引擎生成
 * @author: Shann 851188611@qq.com
 */

namespace app\admin\controller;

class UserBase
{
    public function index()
    {
        $users = \app\model\User::field(['id', 'username', 'real_name', 'phone', 'avatar_file_id', 'created_at', 'updated_at']);

        $users->order('id', 'desc');

        $users->with(['avatar' => function ($query) {
            $query->field(['id', 'url']);
        }]);

        $data['users'] = $users->select();

        return json(['code' => 20000, 'data' => $data]);
    }

    public function detail()
    {
        $user = \app\model\User::field(['id', 'username', 'real_name', 'phone', 'avatar_file_id']);

        $user->where('id', input('id'));

        $user->with(['avatar' => function ($query) {
            $query->field(['id', 'url']);
        }]);

        $data['user'] = $user->find();

        return json(['code' => 20000, 'data' => $data]);
    }

    public function create()
    {
        $param = request()->param();

        $model = new \app\model\User();

        $model->save($param);

        $data['user'] = \app\model\User::find($model->id);

        return json(['code' => 20000, 'msg' => 'success', 'data' => $data]);
    }

    public function update()
    {
        $param = request()->param();

        $model = \app\model\User::find(input('id'));

        $model->save($param);

        return json(['code' => 20000, 'msg' => 'success']);
    }

    public function delete()
    {
        \app\model\User::destroy(function ($query) {
            $query->where('id', input('id'));
        });

        return json(['code' => 20000, 'msg' => 'success']);
    }
}
